Xbasic

Extension::JsonJsonAddFields Method

Syntax

.JsonAddFields as c (json as C, jsonFieldtemplate as C)

Arguments

jsonCharacter

JSON to process.

jsonFieldtemplateCharacter

Template with 'shape' of JSON to process, including fields to add. In addition to fields specified by {<fieldname>}, there are built fields, described below.

{./begincurly}

Emit a '{' in the template.

{./endcurly}

Emit a '}' in the template.

{./number}

When included, Emit this field as a numeric value rather than string value.

{./logical}

When included, Emit this field as a logical value rather than string value.

{./replace}

When included, indicate that this field replaces an existing field (remove the field if it exists).

{./delete}

The field named is not emitted, but instead causes an existing field to be removed.

{./wrap}

Emits an object field that 'contains' all the fields that follow (including the existing fields).

{./row}

Emit counter for the 'row' in the current branch.

{./count}

Emit the number of rows in the current branch.

{./index}

Emit counter for the 'row' from that first branch to the current location.

{./start}

Emit the 'index' for the first row in the current branch.

{./rowz}

Emit counter for the 'row' in the current branch, but assume numbering starts a '0' instead of '1'.

{./indexz}

Emit counter for the 'row' from that first branch to the current location, but assume numbering starts a '0' instead of '1'.

{./startz}

Emit the 'index' for the first row in the current branch, but assume numbering starts a '0' instead of '1'.

{./<child>/./start}

Emit the 'start' Index of the first entry in a specified child array field.

{./<child>/./count}}

Emit the count of the entries in a specified child array field.

{./<child>/./startz}

Emit the 'start' Index of the first entry in a specified child array field, but assume numbering starts a '0' instead of '1'.

Description

Adds fields from a template that matches the 'shape' of the input json.

Discussion

This function allows for fields to be added, populated using a template. The template is populated from field references enclosed in '{}', parent object fields can be pulled in using a "../" prefix on the field, multiple "../" prefix in front of a field can look up multiple levels (i.e. grandparent).

Example

dim schema as c 
schema = <<%str%
[
    {
        "orders" : [
            {
                "RecordId" : "{../CustomerID} {OrderId}",                
                "OrderItems" : [
                  {
                      "OrderID" : "{../OrderId}"
                  }
                ]
            }
        ]
    }
]
%str%

dim json as c = <<%str%
[
        {
            "CustomerID": "ALFKI",
            "CompanyName": "Alfreds Futterkistes",
            "orders": [
                {
                    "OrderID": "10643",
                    "OrderDate": "08/25/1997 12:00:00 000 am",
                    "orderItems": [
                        {
                            "ProductID": "28",
                            "Quantity": "15",
                            "UnitPrice": "2"
                        },
                        {
                            "ProductID": "39",
                            "Quantity": "21",
                            "UnitPrice": "18"
                        }
                    ]
                } 
            ]
        }
]
%str%

dim out as c = extension::JSON::JsonAddFields(json,schema)
? out
[
    {
        "CustomerID": "ALFKI",
        "CompanyName": "Alfreds Futterkistes",
        "orders": [
            {
                "RecordId": "ALFKI 10643",
                "OrderID": "10643",
                "OrderDate": "08/25/1997 12:00:00 000 am",
                "orderItems": [
                    {
                        "OrderID": "10643",
                        "ProductID": "28",
                        "Quantity": "15",
                        "UnitPrice": "2"
                    },
                    {
                        "OrderID": "10643",
                        "ProductID": "39",
                        "Quantity": "21",
                        "UnitPrice": "18"
                    }
                ]
            }
        ]
    }
]

Escaped Curly Brace Support

If you want to generate curly braces in the template, you can use the {./begincurly} and {./endcurly} built in template fields.

dim json as c = <<%str%
[
   { "id" : "a" },
   { "id" : "b" }   
]
%str%
dim tmpl as c = <<%str%
[
  {
    "IdInBrackets" : "{./begincurly}{id}{./endcurly}"
  }
]
%str%
dim out as c = extension::JSON::JsonAddFields(json,tmpl)
? json_reformat(out)
[
    {
        "IdInBrackets": "{a}",
        "id": "a"
    },
    {
        "IdInBrackets": "{b}",
        "id": "b"
    }
]

Row Counter support

The {./row} builtin template field Adds a row counter that starts at '1'. If you want a row counter that starts a '0' use the {./rowz} builtin template field instead.

dim json as c = <<%str%
[
   { "id" : "a" ,
     "kids" : [
        { "subid" : "aa" },
        { "subid" : "ab" }
     ]
   },
   { "id" : "b" ,
     "kids" : [
        { "subid" : "ba" },
        { "subid" : "bb" },
        { "subid" : "bc" }
     ]
   }   
]
%str%
dim tmpl as c = <<%str%
[
  {
     "kids" : [
       {
         "index" : "{./row}"
       }
     ]
  }
]
%str%
dim out as c = extension::JSON::JsonAddFields(json,tmpl)
? json_reformat(out)
[
    {
        "id": "a",
        "kids": [
            {
                "index": "1",
                "subid": "aa"
            },
            {
                "index": "2",
                "subid": "ab"
            }
        ]
    },
    {
        "id": "b",
        "kids": [
            {
                "index": "1",
                "subid": "ba"
            },
            {
                "index": "2",
                "subid": "bb"
            },
            {
                "index": "3",
                "subid": "bc"
            }
        ]
    }
]

Consecutive Row Counter support

The {./index} builtin template field Adds a row counter that starts at '1', the difference between {./index} and {./row} is that while {./row} is reset for every new child, {./index} never gets reset so it reflects the Consecutive row for all records of the same type. If you want a row counter that starts a '0' use the {./indexz} builtin template field instead.

'Date Created: 14-Feb-2017 11:00:55 AM
'Last Updated: 14-Feb-2017 11:00:55 AM
'Created By  : Cian
'Updated By  : Cian
dim json as c = <<%str%
[
   { "id" : "a" ,
     "kids" : [
        { "subid" : "aa" },
        { "subid" : "ab" }
     ]
   },
   { "id" : "b" ,
     "kids" : [
        { "subid" : "ba" },
        { "subid" : "bb" },
        { "subid" : "bc" }
     ]
   }   
]
%str%
dim tmpl as c = <<%str%
[
  {
     "kids" : [
       {
         "index" : "{./index}"
       }
     ]
  }
]
%str%
dim out as c = extension::JSON::JsonAddFields(json,tmpl)
? json_reformat(out)
[
    {
        "id": "a",
        "kids": [
            {
                "index": "1",
                "subid": "aa"
            },
            {
                "index": "2",
                "subid": "ab"
            }
        ]
    },
    {
        "id": "b",
        "kids": [
            {
                "index": "3",
                "subid": "ba"
            },
            {
                "index": "4",
                "subid": "bb"
            },
            {
                "index": "5",
                "subid": "bc"
            }
        ]
    }
]

Start Index and Row Count Support

The {./start} and {./count} builtin template fields retreive the first 'index' in the current set of rows, and the number of rows in the current set of rows. If zero based index is preferred use the{./startz} built in field.

dim json as c = <<%str%
[
   { "id" : "a" ,
     "kids" : [
        { "subid" : "aa" },
        { "subid" : "ab" }
     ]
   },
   { "id" : "b" ,
     "kids" : [
        { "subid" : "ba" },
        { "subid" : "bb" },
        { "subid" : "bc" }
     ]
   }   
]
%str%
dim tmpl as c = <<%str%
[
  {
     "kids" : [
       {
          "index" : "{./index}",
          "start" : "{./start}",
          "count" : "{./count}"
       }
     ]
  }
]
%str%
dim out as c = extension::JSON::JsonAddFields(json,tmpl)
? json_reformat(out)	
[
    {
        "id": "a",
        "kids": [
            {
                "index": "1",
                "start": "1",
                "count": "2",
                "subid": "aa"
            },
            {
                "index": "2",
                "start": "1",
                "count": "2",
                "subid": "ab"
            }
        ]
    },
    {
        "id": "b",
        "kids": [
            {
                "index": "3",
                "start": "3",
                "count": "3",
                "subid": "ba"
            },
            {
                "index": "4",
                "start": "3",
                "count": "3",
                "subid": "bb"
            },
            {
                "index": "5",
                "start": "3",
                "count": "3",
                "subid": "bc"
            }
        ]
    }
]

Count and Start of a Child

The {./<child>/./start} and {./<child>/./count} builtin template fields retreive the first 'index' in the specified child of the current row, and the number of rows in the specified child. If zero based index is preferred use the{./<child>/./startz} built in field.

dim json as c = <<%str%
[
   { "id" : "a" ,
     "kids" : [
        { "subid" : "aa" },
        { "subid" : "ab" }
     ]
   },
   { "id" : "b" ,
     "kids" : [
        { "subid" : "ba" },
        { "subid" : "bb" },
        { "subid" : "bc" }
     ]
   }   
]
%str%
dim tmpl as c = <<%str%
[
  {
     "kidstart" : "{./number}{./kids/./start}",
     "nkid" : "{./number}{./kids/./count}",
     "kids" : [
       {
       }
     ]
  }
]
%str%
dim out as c = extension::JSON::JsonAddFields(json,tmpl)
? json_reformat(out)
[
    {
        "kidstart": 1,
        "nkid": 2,
        "id": "a",
        "kids": [
            {
                "subid": "aa"
            },
            {
                "subid": "ab"
            }
        ]
    },
    {
        "kidstart": 3,
        "nkid": 3,
        "id": "b",
        "kids": [
            {
                "subid": "ba"
            },
            {
                "subid": "bb"
            },
            {
                "subid": "bc"
            }
        ]
    }
]